home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
opndor
/
ex_vote.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-02-26
|
47KB
|
1,297 lines
/* EX_VOTE.C - This program demonstrates an online voting program that is */
/* written using OpenDoors. The Vote program allows users to */
/* create questions or surveys for other users to respond to. */
/* Users are also able to view the results of voting on each */
/* topic. The program supports up to 200 questions, and can be */
/* configured to either allow or disallow viewing of results */
/* prior to voting. */
/* */
/* This program shows how to do the following: */
/* */
/* - How to display text using od_printf(), using imbedded */
/* strings to change the display color. */
/* - Add support for standard command-line options. */
/* - Use the OpenDoors configuration file system, including */
/* adding your own configuration file options. */
/* - Activate the OpenDoors log file system and write */
/* information to the log file. */
/* - Display a menu from an external ASCI/ANSI/Avatar/RIP */
/* file. */
/* - How to setup a user file and general data file, and how */
/* to access it in a multi-node compatible way (if */
/* MULTINODE_AWARE is defined). */
/* */
/* To recompile this program, follow the instructions in the */
/* OpenDoors manual. For a DOS compiler, be sure to set your */
/* compiler to use the large memory model, and add the */
/* ODOORL.LIB file to your project/makefile. */
/* Uncomment the following line for multi-node compatible file access. */
/* #define MULTINODE_AWARE */
/* Include standard C header files required by Vote. */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <ctype.h>
#ifdef MULTINODE_AWARE
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <share.h>
#endif
/* Include the OpenDoors header file. This line must be done in any program */
/* using OpenDoors. */
#include "opendoor.h"
/* Manifest constants used by Vote */
#define NO_QUESTION -1
#define NEW_ANSWER -1
#define QUESTIONS_VOTED_ON 0x0001
#define QUESTIONS_NOT_VOTED_ON 0x0002
#define MAX_QUESTIONS 200
#define MAX_USERS 30000
#define MAX_ANSWERS 15
#define QUESTION_STR_SIZE 71
#define ANSWER_STR_SIZE 31
#define USER_FILENAME "VOTE.USR"
#define QUESTION_FILENAME "VOTE.QST"
#define FILE_ACCESS_MAX_WAIT 20
#define QUESTION_PAGE_SIZE 17
/* Structure of records stored in the VOTE.USR file */
typedef struct
{
char szUserName[36];
BYTE bVotedOnQuestion[MAX_QUESTIONS];
} tUserRecord;
tUserRecord CurrentUserRecord;
int nCurrentUserNumber;
/* Structure of records stored in the VOTE.QST file */
typedef struct
{
char szQuestion[72];
char aszAnswer[MAX_ANSWERS][32];
INT32 nTotalAnswers;
DWORD auVotesForAnswer[MAX_ANSWERS];
DWORD uTotalVotes;
DWORD bCanAddAnswers;
char szCreatorName[36];
time_t lCreationTime;
} tQuestionRecord;
/* Global variables. */
int nViewResultsFrom = QUESTIONS_VOTED_ON;
int nQuestionsVotedOn = 0;
/* Prototypes for functions that form EX_VOTE */
void CustomConfigFunction(char *pszKeyword, char *pszOptions);
void BeforeExitFunction(void);
void VoteOnQuestion(void);
void ViewResults(void);
int GetQuestion(int nQuestion, tQuestionRecord *pQuestionRecord);
void AddQuestion(void);
int ChooseQuestion(int nFromWhichQuestions, char *pszTitle, int *nLocation);
void DisplayQuestionResult(tQuestionRecord *pQuestionRecord);
int ReadOrAddCurrentUser(void);
void WriteCurrentUser(void);
FILE *ExclusiveFileOpen(char *pszFileName, char *pszMode, int *phHandle);
void ExclusiveFileClose(FILE *pfFile, int hHandle);
void WaitForEnter(void);
/* main() or WinMain() function - Program execution begins here. */
#ifdef ODPLAT_WIN32
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
#else
int main(int argc, char *argv[])
#endif
{
/* Variable to store user's choice from the menu */
char chMenuChoice = '\0';
char chYesOrNo;
#ifdef ODPLAT_WIN32
/* In Windows, pass in nCmdShow value to OpenDoors. */
od_control.od_cmd_show = nCmdShow;
/* Ignore unused parameters. */
(void)hInstance;
(void)hPrevInstance;
#endif
/* Set program's name for use by OpenDoors. */
strcpy(od_control.od_prog_name, "Vote");
strcpy(od_control.od_prog_version, "Version 6.00");
strcpy(od_control.od_prog_copyright, "Copyright 1991-1996 by Brian Pirie");
/* Call the standard command-line parsing function. You will probably */
/* want to do this in most programs that you write using OpenDoors, as it */
/* automatically provides support for many standard command-line options */
/* that will make the use and setup of your program easer. For details, */
/* run the vote program with the /help command line option. */
#ifdef ODPLAT_WIN32
od_parse_cmd_line(lpszCmdLine);
#else
od_parse_cmd_line(argc, argv);
#endif
/* Enable use of OpenDoors configuration file system. */
od_control.od_config_file = INCLUDE_CONFIG_FILE;
/* Set function to process custom configuration file lines. */
od_control.od_config_function = CustomConfigFunction;
/* Include the OpenDoors multiple personality system, which allows */
/* the system operator to set the sysop statusline / function key set */
/* to mimic the BBS software of their choice. */
od_control.od_mps = INCLUDE_MPS;
/* Include the OpenDoors log file system, which will record when the */
/* door runs, and major activites that the user performs. */
od_control.od_logfile = INCLUDE_LOGFILE;
/* Set filename for log file. If not set, DOOR.LOG will be used by */
/* default. */
strcpy(od_control.od_logfile_name, "VOTE.LOG");
/* Set function to be called before program exits. */
od_control.od_before_exit = BeforeExitFunction;
/* Initialize OpenDoors. This function call is optional, and can be used */
/* to force OpenDoors to read the door informtion file and begin door */
/* operations. If a call to od_init() is not included in your program, */
/* OpenDoors initialization will be performed at the time of your first */
/* call to any OpenDoors function. */
od_init();
/* Call the Vote function ReadOrAddCurrentUser() to read the current */
/* user's record from the Vote user file, or to add the user to the */
/* file if this is the first time that they have used Vote. */
if(!ReadOrAddCurrentUser())
{
/* If unable to obtain a user record for the current user, then exit */
/* the door after displaying an error message. */
od_printf("Unable to access user file. File may be locked or full.\n\r");
WaitForEnter();
od_exit(1, FALSE);
}
/* Loop until the user choses to exit the door. For each iteration of */
/* this loop, we display the main menu, get the user's choice from the */
/* menu, and perform the appropriate action for their choice.